<template>
	<view class="page">
		<view class="topprice">
			账户可用洁净币 ¥{{wallet.balance || 0}} 冻结金额 ¥{{wallet.freezeBalance || 0}}
		</view>
		<view class="bg"> </view>

		<view class="adress" @click="tourl('/pages/my/myadress/myadress?type=add')">
			<view class="" v-if="adressinfo" >
				<view class="a1">{{adressinfo.name}} {{adressinfo.mobile}}</view>
				<view class="a2">{{adressinfo.areaAddress}}{{adressinfo.detailAddress}} </view>
			</view>
			<view class="" v-if="!adressinfo">
				<view class="a1">请输入您的详细服务地址</view>
				<view class="a2">您当前还没有地址信息哦</view>
			</view>
			<u-icon slot="right" name="arrow-right"></u-icon>

		</view>
		<!--保洁单1 工程单2 陪护单3 -->
		
		<view class="form">
			<!-- {{orderType}} -->
				<view class="list">
					<view class="title">服务项目：</view>
					<view class="inp" @click="categoryDia=true" v-if="serveInfo.categoryName">
						{{serveInfo.categoryName }} <u-icon slot="right" name="arrow-right"></u-icon>
					</view>
					<view class="inp" @click="categoryDia=true" v-else>
						<text style="color: #AAAAAA;">请选择服务项目</text><u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list"  v-if="orderType==2">
					<view class="title">数量：</view>
					<view class="inp" >
						<input type="number" v-model="serveInfo.num" style="text-align: right;" placeholder="请输入数量"/> 
						<u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list"   >
					<view class="title">服务日期：</view>
					<view class="inp" @click="showdate(1)" v-if="serveInfo.serviceStartTime">
						<text v-if="orderType!=2">{{serveInfo.serviceStartTime}}-{{serveInfo.serviceEndTime}} {{serveInfo.serviceDate || ''}}</text>
						<text v-else>{{serveInfo.serviceStartTime}} {{serveInfo.serviceDate || ''}}</text>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</view>
					<view class="inp" @click="showdate(1)" v-else>
						<text style="color: #AAAAAA;">请选择服务日期</text>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list" v-if="orderType==1"  @click="showTimePopup">
					<view class="title">服务时间：</view>
					<view class="inp" >
						{{ serveInfo.timeCycle || ""}}
						<u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list" v-if="orderType==3"   >
					<view class="title">时间周期：</view>
					<view class="inp">
						<input type="number" v-model="serveInfo.days" :disabled="true" style="text-align: right;" placeholder="请输选择服务日期"/> 
						 
					</view>
				</view>
				<view class="list" v-if="orderType==3"   >
					<view class="title">休息天数：</view>
					<view class="inp">
						<input type="number" v-model="serveInfo.restDays" style="text-align: right;" placeholder="请输入休息天数"/> 
						 
					</view>
				</view>
				<view class="list" v-if="orderType==2 || orderType==3"   >
					<view class="title">截止接单时间：</view>
					<view class="inp"  @click="showdate(2)">
						{{serveInfo.deadlineTime}} 
						<u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list" v-if="orderType==2 || orderType==3">
					<view class="title">服务人员：</view>
					<view class="inp"   @click="showperson(0)">
						<text v-if="orderStaffDO.length==0">选择人员</text>
						<view v-else>
							<text v-for="(item) in orderStaffDO" :key="item.name">{{item.name}}</text>
						</view>
						 
						 <u-icon slot="right" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="list" v-if="orderType==1">
					<view class="title">服务人数：</view>
					<view class="inp"  >
						 
						<u-radio-group v-model="serveInfo.serviceNum" @change="changeServiceNum"
						style="justify-content: space-between;">
							<u-radio v-for="(item, index) in radioList" :key="index" :name="item.value"
								:customStyle="{marginLeft: '38rpx'}"  :label="item.name">
								 
							</u-radio>
						</u-radio-group>
					</view>
				</view>
				<template v-if="orderType==1">
				
					<view class="list" v-for="(item,index) in orderStaffDO" :key="item.name">
						<view class="title">{{'服务人员'+(index+1)}}：</view>
						<view class="inp" @click="showperson(index)">
							<text v-if="orderStaffDO.length==0">选择人员</text>
							<view v-else>
								<text >{{item.name}}</text>
							</view>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</view>
					</view>
				</template>
				<view class="list"  >
					<view class="title">工具使用：</view>
					<view class="inp"  >
						<u-radio-group v-model="serveInfo.toolUsage" style="justify-content: flex-end;">
							<u-radio v-for="(item, index) in toolUsageList" :key="index" :name="item.name"
								:customStyle="{marginLeft: '38rpx'}"  :label="item.name">
								 
							</u-radio>
						</u-radio-group>
					</view>
				</view>
			 
		</view>
		
		<view class="form">
			<view class="upimage">
				<view class="title2">
					上传现场照片：<text>便于服务人员携带工具（最多上传3张）</text>
					</view>
				<view class="up3">
					<view class="img" v-for="(item,index) in images" :key="index">
						<image :src="item" class="image"  @click="lookImg(index)"></image>
						<view class="del">
							<u-icon slot="right" name="close" @click="delimg(index)" size="12"></u-icon>
							
						</view>
 					</view>
					<view class="add" @click="addimg()" v-if="images.length<3">
						<view>+</view>
					</view>
				</view>			
			</view>
			<view class="flex" style="justify-content: space-between;">
				<view class="title2"> 客户留言：  </view>
				<view class="flex" style="flex: 1;">
					<input type="text" v-model="serveInfo.remark" placeholder="选填" style="text-align: right;width: 100%;"> 
					<!-- <u-icon slot="right" name="arrow-right"   ></u-icon> -->
				</view>			
			</view>
		</view>
		<view class="form" v-if="orderType==1">
			 
			<view class="flex" style="justify-content: space-between;">
				<view class="title2"> 是否有宠物：  </view>
				<view class="flex">
					<u-radio-group v-model="serveInfo.isPet" style="justify-content: flex-end;">
						<u-radio v-for="(item, index) in flagPet" :key="index" :name="item.id"
							:customStyle="{marginLeft: '38rpx'}">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</view>			
			</view>
		</view>
		<view class="tip" v-if="type==2">
			*工程类服务订单具体上门时间由接单师傅跟客户沟通协调， 下单成功后请您留意接听电话！
		</view>
		
	
		
		 <u-popup :show="footvisible" :round="10" mode="bottom" :closeOnClickOverlay="true" :closeable="true" 
		 :zIndex="10070" @close="footvisible=false">
		 	<view class="popup">
		 		<view class="tip">
		 			 <text>价格明细</text>
		 		</view>
		 		<view style="margin-top:30rpx;">
		 			<view class="box">
		 				 <!-- 0 平台劵 1 员工劵 2 商户劵 3 商户给员工的优惠劵 -->
		 				<view class="a">
		 					<view style="font-size: 32rpx;">服务总价</view> 
		 					<text style="font-size: 32rpx;">￥{{totalMoney || '-'}}</text>
		 				</view> 
		 				<view class="a" @click="choseCoupon(1,couponList1)">
		 					<view style="font-weight: 400;">平台优惠券</view> 
		 					<view class="flex" v-if="couponList1.length>0" style="color: #277EEF;font-size: 36rpx;">
								<text class="rmb">减¥</text> {{serveInfo.discountSystemPrice }}
								<u-icon slot="right" name="arrow-right"></u-icon>
							</view>
							<view v-else>
								 <text>暂无可用优惠券</text>
							</view>
		 				</view>  
		 				<view class="a" @click="choseCoupon(2,couponList2)">
		 					<view style="font-weight: 400;">商户优惠券</view> 
		 					<text class="flex" v-if="couponList2.length>0" style="color: #277EEF;font-size: 36rpx;">
								<text class="rmb">减¥</text>{{serveInfo.discountMerchantPrice || "0"}}
								<u-icon slot="right" name="arrow-right"></u-icon>
							</text>
							<view v-else>
								 <text>暂无可用优惠券</text>
							</view>
		 				</view> 
		 				<view class="a" @click="choseCoupon(3,couponList3)">
		 					<view style="font-weight: 400;">员工优惠券</view> 
		 					<view class="flex"  v-if="couponList3.length>0" style="color: #277EEF;font-size: 36rpx;">
								<text class="rmb">减¥</text>{{serveInfo.discountStaffPrice || '0'}}
								<u-icon slot="right" name="arrow-right"></u-icon>
							</view> 
							<view v-else>
								 <text>暂无可用优惠券</text>
							</view>
		 				</view> 
		 				<view class="a">
		 					<view style="font-weight: 400;">交通补贴</view> 
		 					<text>{{serveInfo.subsidyMoney || '-'}}</text>
		 				</view> 
		 				<view class="a" style="font-size: 20rpx;color: #AAAAAA;margin-top: -2rpx;">
		 					 {{transportation}}
		 				</view>  
		 				<view class="a">
		 					<view>总计</view> 
							<view style="color: #277EEF;">
								￥<text style="font-size: 40rpx;color: #277EEF;">{{actualMoney || 0}}</text>
							</view> 
		 				</view> 
		 				 
		 			</view> 
		 			 
		 		</view>
		 		 
		 	</view>
		 </u-popup> 
		  
		<view class="orderfooter" style="z-index: 10071;" v-if="!serveInfo.categoryName">
			  <text>请选择您需要的服务进行预约！</text>
			  <view>提交申请</view>
		</view>
		<view class="orderfooter" style="z-index: 10071;" v-else>
		  <view class="fn-col-left">
			<view class="col-left">
			  <view>{{ serveInfo.serviceNum || 0}}人服务</view>
			  <view>共{{ totalDayNum || 0}}天</view>
			</view>
			<view class="col-right">
			  <view class="price-number">
				总计 ¥ <span class="price">{{ actualMoney || 0}}</span>
			  </view>
			  <view class="price-detaile" @click="showActions">
				价格明细 <uni-icons :type="showPrice ? 'bottom' : 'top'" size="16"></uni-icons>
			  </view>
			</view>
		  </view>
		  <view class="woodsbtn" @click="handleSubmit">
			立即预约
		  </view>
		</view>
		
		<wuCalendar ref="calendar" @confirm="confirm" :range="mode"/>
		<chose-time ref="choseTime" @handleTime="handleTime"></chose-time>
		
		<chose-person :categoryId="serveInfo.categoryId" ref="choseperson" @handleChose="handleChose"></chose-person>
		<price-info ref="priceInfo"></price-info>
		<goodsSelect @chose="choseCategory" :show="categoryDia" />
		<couponModal ref="couponModal" @handleChoseCoupon="handleChoseCoupon"/>
	</view>
</template>

<script>
	import {mapMutations,mapState } from "vuex";
	import { calculateDaysBetweenDates,findBestCoupon,timeToDecimal,countSpecificWeekdays} from "@/utils/utils.js"
	import pro from "@/utils/uploadImg.js"
	import choseTime from "@/components/choseTime"
	import goodsSelect from "./components/goodsSelect.vue"
	import priceInfo from "./components/priceInfo.vue"
	import chosePerson from "@/components/chose-person.vue"
	import {goodsDetailsAPI,settlementLongAPI} from "@/api/appointment.js"
	import {addressdefaultlistAPI,getwalletAPI,couponListAPI} from "@/api/user.js"
	import {createOrderAPI,settlementAPI,categoryListAPI,orderdetailAPI,transportationAllowanceAPI,
	getMerchantCouponAPI,getStaffCouponAPI,systemCouponAPI} from "@/api/order.js"
	import wuCalendar from "./components/wu-calendar.vue"
	import {formatDate} from "@/utils/base.js"
	import couponModal from "./components/couponModel.vue"
	export default {
		components: {
			choseTime,chosePerson,priceInfo,goodsSelect,wuCalendar,couponModal
		},
		data() {
			return {
				footvisible:false,
				showPrice:false,
				show: false,
				mode: false,//是否区间选择
				calendarType:1,//1.服务时间2.截止时间
				toolUsageList:[
					{ name: '服务人员自带' },{ name: '客户自备' },
				],
				flagPet: [
					{ name: '有', id:1 },
					{ name: '没有', id:0},
				],
				radioList: [
					{ name: '1人', value: 1 },
					{ name: '2人', value: 2 },
					{ name: '3人', value: 3 }, 
				],
				 
				images: [],
				wallet:{//钱包
					
				},
				categoryId:"",//服务项目id
				serveInfo:{ //服务对象
					categoryName:"",
					serviceStartTime:"",
					serviceEndTime:"",
					serviceNum:"",
					timeCycle:"",
					serviceDate:"",
					isPet:"0",
					restDays:"",
					days:"",
					num:"",
					deadlineTime:"",
					remark:""
				},
				money:0, 
				orderStaffDO:[],
				orderType:"",//1 保洁单 2 工程单 3 陪护单
				 
				couponList1:[],//0 平台劵
				couponList2:[],//员工劵
				couponList3:[],//商户券
				categoryDia:false,
				totalDayNum:0,//保洁单专用-选中的工程单天数
				personIndex:0,
				oldId:"",
				editNum:1,
				userAICoupon:{
					discountSystem:true,
					discountMerchant:true,
					discountStaff:true
				},
				actualMoney:0,
				transportation:"",
				staff:{
					
				}
			}
		},
		watch:{
			adressinfo: {
				handler(newVal) {
					if (newVal && newVal.longitude && newVal.latitude) {
						this.changeServiceNum(this.serveInfo.serviceNum)
						this.editNum=this.editNum+1
						this.$nextTick(() => {
							if (this.$refs.choseperson) {
							this.$refs.choseperson.addressLocation = {
								longitude: newVal.longitude,
								latitude: newVal.latitude,
							};
							}
						});
					}
				},
				deep: true,
				immediate: true,
			},
			totalMoney:{
				handler:async function(money){
			 
				// 0 平台劵 1 员工劵3 商户给员工的优惠劵  2 商户劵
					if(!money || money==0){
						// this.serveInfo.discountSystemPrice=0
						// this.serveInfo.discountMerchantPrice=0
						// this.serveInfo.discountStaffPrice=0
						return
					}
					await this.getsystemCoupon(money,this.serveInfo.oneCategoryId)
					if(this.serveInfo.serviceNum==1 && this.orderStaffDO.length==1 && this.orderStaffDO[0].staffId){
						await this.getMerAndStaCoupon(this.totalMoney,this.orderStaffDO[0].staffId)
					}else{
						this.serveInfo.discountStaffPrice=0
						this.serveInfo.staffCouponsId=0
						this.serveInfo.discountMerchantPrice=0
						this.serveInfo.merchantCouponsId=0
					}
					
					this.getactualMoney()
					
				},
				immediate: false,
			}
		},
		 
		computed:{
		    ...mapState("common", ["adressinfo"]),
			// 服务单总价（不包含补贴）
			totalMoney(){
				let total=0
				if(!this.money || !this.serveInfo.serviceStartTime){
					return total
				}
				
				// 1 保洁单 2 工程单 3 陪护单
				switch (this.orderType){
					// 保洁单总价=单价*人天小时数*工作天数*人数
					case "1":
					case 1:
						if(this.money && this.serveInfo.serviceSection && this.totalDayNum && this.serveInfo.serviceNum){
							total=this.serveInfo.serviceSection*this.money*this.totalDayNum*this.serveInfo.serviceNum			
						}
						break;
					// 工程单总价=单价*台数
					case "2":
					case 2:
						if(this.money && this.serveInfo.num){
							total=this.serveInfo.num*this.money 
						}
						break;
					//  陪护单总价=单价*（天数-休息数）
					case "3":
					case 3:	
						if(this.money && this.serveInfo.restDays!='' && this.serveInfo.days!=''){
							total=(this.serveInfo.days-this.serveInfo.restDays)*this.money
						}
					
						break;
					
				}
				return total 
			},
			
			
		},
		created() {
			this.getinit()
			
		},
		onLoad(option) {
			if(option.categoryId){
				this.categoryId=option.categoryId
				this.goodsDetails()
			}
			if(option.oneCategoryName){
				this.serveInfo.oneCategoryId=option.oneCategoryId
				this.serveInfo.categoryId=option.categoryId
				this.orderType=this.getCategoryIndex(option.oneCategoryName)
				if( this.orderType=='2' || this.orderType==3){
					this.serveInfo.serviceNum =1
				}else{
					this.serveInfo.serviceNum =1
					this.changeServiceNum(1)
				}
				if(option.staffId){
					
					 this.staff={
						 staffId:option.staffId,
						 remark:"",
						 merchantId:option.merchantId,
						 name:option.staffName,
						 subsidyMoney:0
					 }
					
				}
			}
			
			if(option.oldId){
				this.oldId=option.oldId
				this.orderDetails()
			}
		},
		methods: {
			...mapMutations("common",["SET_adressinfo"]),
			// 实付=总价-优惠券+补贴
			getactualMoney(){
				let total=this.totalMoney || 0
				 
				if(!this.money || !this.serveInfo.serviceStartTime){
					return total
				}
				if(this.serveInfo.discountSystemPrice || this.serveInfo.discountSystemPrice==0){
					total=total - this.serveInfo.discountSystemPrice 
					
				}
				if(this.serveInfo.discountStaffPrice || this.serveInfo.discountStaffPrice==0){
					total=total - this.serveInfo.discountStaffPrice
				}
				if(this.serveInfo.discountMerchantPrice || this.serveInfo.discountMerchantPrice==0){
					total=total - this.serveInfo.discountMerchantPrice
				}
				// 
				let subsidyMoney=0
				if(this.orderStaffDO.length){
					//total=Number(total) + Number(this.serveInfo.subsidyMoney)*serveInfo.serviceNum
					for (var i = 0; i < this.orderStaffDO.length; i++) {
						subsidyMoney+=this.orderStaffDO[0]?.subsidyMoney?this.orderStaffDO[0]?.subsidyMoney:0
					}
				}
				if(this.orderType==3){
					subsidyMoney=(this.serveInfo.days-this.serveInfo.restDays)*subsidyMoney
				}
				if(this.orderType==1){
					subsidyMoney=(this.totalDayNum)*subsidyMoney
				}
				this.serveInfo.subsidyMoney=subsidyMoney
				this.actualMoney=total+subsidyMoney
			},
			handleChoseCoupon(item,n){
				switch (n){
					case 1: 
						this.$set(this.serveInfo,"discountSystemPrice",item.discountPrice)
						this.serveInfo.systemCouponsId=item.id
					break;
					case 3:
						this.$set(this.serveInfo,"discountStaffPrice",item.discountPrice)
						this.serveInfo.staffCouponsId=item.id
					break;
					case 2:
						this.$set(this.serveInfo,"discountMerchantPrice",item.discountPrice)
						this.serveInfo.merchantCouponsId=item.id 
					break;
				}
				this.getactualMoney()
			},
			choseCoupon(n,list){
				this.$refs.couponModal.getcoupon(n,list,this.totalMoney)
			},
			// 订单详情
			orderDetails(){
				orderdetailAPI({id:this.oldId}).then(res=>{
					
					this.serveInfo=res
					this.serveInfo.subsidyMoney=""
					this.serveInfo.discountStaffPrice=""
					this.serveInfo.discountMerchantPrice=""
					this.serveInfo.discountSystemPrice=""
					this.serveInfo.staffCouponsId=''
					this.serveInfo.merchantCouponsId=''
					this.serveInfo.systemCouponsId=''
					this.money=res.money 
					this.orderType=res.type+''
					this.images=res.images?res.images.split(",") :[]
					this.orderStaffDO=res.orderStaffDOS.map(item=>{
						return {
							  "remark": "",
							  "staffId": item.staffId,
							  "merchantId": item.merchantId,
							  "isAssign": 1,
							  name:item.name
						}
					})
					 
					if(this.orderType==1){
						console.log(res.serviceDate)
						if(res.serviceDate){
							this.totalDayNum=countSpecificWeekdays(new Date(res.serviceStartTime),new Date(res.serviceEndTime),res.serviceDate.split(","))
						}else{
							this.totalDayNum=calculateDaysBetweenDates(res.serviceStartTime,res.serviceEndTime)
						}
					} else if(this.orderType==2){
						this.totalDayNum=1
						this.serveInfo.deadlineTime=formatDate(res.deadlineTime,'YYYY-MM-DD HH:mm')
					}else if(this.orderType==3){
						this.totalDayNum=calculateDaysBetweenDates(res.serviceStartTime,res.serviceEndTime)
						this.serveInfo.deadlineTime=formatDate(res.deadlineTime,'YYYY-MM-DD HH:mm')
					}
					
					this.SET_adressinfo({
						name:res.receiverName,
						mobile:res.receiverMobile,
						detailAddress:res.receiverDetailAddress,
						areaAddress:res.areaAddress,
						id:res.addressId,
						longitude:res.longitude,
						latitude:res.latitude,
						receiverAreaId:res.receiverAreaId,
					}) 
					delete this.serveInfo.id
				})
			},
		 
			changeServiceNum(val){
				// const match = str.match(/(\d+)/);
				 console.log(val)
				let serve={
				  staffId: '', 
				  name:"随机分配"
				}
				this.orderStaffDO=Array.from({ length: val}, () => serve);
			},
			choseCategory(checkedItem,checkedIndex,flag){
				this.categoryDia = false;
				// 点击确认并选择了数据时
				if (flag && checkedIndex !== "") {
					this.serveInfo={  
						categoryName:"",
						serviceStartTime:"",
						serviceEndTime:"",
						serviceNum:"",
						timeCycle:"",
						serviceDate:"",
						isPet:"0",
						restDays:"",
						days:"",
						num:"",
						deadlineTime:"",
						remark:""
					}
				   this.serveInfo.oneCategoryId=checkedItem.oneCategoryId
				   this.serveInfo.categoryId=checkedItem.id
				   this.serveInfo.categoryName=checkedItem.name
				   this.orderType=this.getCategoryIndex(checkedItem.oneCategoryName)
				   
				   this.orderStaffDO=[] 
				   this.money=checkedItem.money  
				   if(this.orderType=='2' || this.orderType==3){
					   this.serveInfo.serviceNum =1
				   }else{
					   this.serveInfo.serviceNum =1
					   this.changeServiceNum(1)
				   }
				   this.actualMoney=0
				}
			},
			async getsystemCoupon(money,categoryId) {
				await  systemCouponAPI({
					money,categoryId
				}).then(res=>{
					this.couponList1= res  
					let system=res.find(item=> item.isFlag)
					if(this.userAICoupon.discountSystem){
						if(system){
							this.serveInfo.discountSystemPrice=system.discountPrice
							this.serveInfo.systemCouponsId=system.id
						}else{
							this.serveInfo.discountSystemPrice=''
							this.serveInfo.systemCouponsId=''
						}
					}
					
				})
			},
			async getMerAndStaCoupon(money,staffId) {
				await getMerchantCouponAPI({money,staffId}).then(res=>{
					this.couponList2= res  
					let system=res.find(item=> item.isFlag)
					if(system){
						this.serveInfo.discountMerchantPrice=system.discountPrice
						this.serveInfo.merchantCouponsId=system.id
					}else{
						this.serveInfo.discountMerchantPrice=''
						this.serveInfo.merchantCouponsId=''
					}
				})
				await getStaffCouponAPI({money,staffId}).then(res=>{
					this.couponList3= res  
					let system=res.find(item=> item.isFlag)
					if(system){
						this.serveInfo.discountStaffPrice=system.discountPrice
						this.serveInfo.staffCouponsId=system.id
					}else{
						this.serveInfo.discountStaffPrice=''
						this.serveInfo.staffCouponsId=''
					}
				})
			},
			handleSubmit(){
				if(!this.adressinfo?.id){
					return uni.showToast({ title:'请选择地址', icon:'error'})
				}
				if(!this.serveInfo?.serviceStartTime){
					return uni.showToast({ title:'请选择服务日期', icon:'error'})
				}
				if(!this.serveInfo?.toolUsage){
					return uni.showToast({ title:'请选择工具使用', icon:'error'})
				}
				uni.showLoading({
					title:'提交订单中'
				}) 
			 


				var data={
					 ...this.serveInfo,

					// deadlineTime:this.serveInfo.deadlineTime,
					// isPet:this.serveInfo.isPet,
					// categoryId:this.serveInfo.categoryId,
					// categoryName:this.serveInfo.categoryName,
					// days:this.serveInfo.days,
					// oneCategoryId:this.serveInfo.oneCategoryId,
					// restDays:this.serveInfo.restDays,
					// serviceDate:this.serveInfo.serviceDate,
					// serviceEndTime:this.serveInfo.serviceEndTime,
					// serviceSection:this.serveInfo.serviceSection,
					// subsidyMoney:this.serveInfo.subsidyMoney,
					// serviceStartTime:this.serveInfo.serviceStartTime,
					// timeCycle:this.serveInfo.timeCycle,
					// toolUsage:this.serveInfo.toolUsage,


					type:this.orderType, 
					addressId:this.adressinfo.id,
					receiverName: this.adressinfo.name,
					receiverMobile:this.adressinfo.mobile,
					receiverAreaId:this.adressinfo.cityCode,
					receiverDetailAddress: this.adressinfo.detailAddress,
					areaAddress:this.adressinfo.areaAddress,
					longitude: this.adressinfo.longitude,
					latitude: this.adressinfo.latitude,
					orderStaffDO:this.orderStaffDO.filter(item=>item.staffId!=''),
					images:this.images.join(","),
					totalMoney:this.totalMoney,
					actualMoney:this.actualMoney,
					isOrder:1,//1 普通订单 2 周期单
					serviceNum:this.orderType!="1"?1:this.serveInfo.serviceNum,
					num:this.orderType=="2"?this.serveInfo.num:1,
					remark:this.serveInfo.remark?this.serveInfo.remark:''
				}
				 
				createOrderAPI(data,this.orderType).then(res=>{
					uni.hideLoading()
					uni.redirectTo({
						url:"/pages/order/payOrder?orderFrom="+this.orderType+"&id="+res.id
					})
				})
			},
			showActions(){
				this.footvisible=!this.footvisible
			},
			async handleChose(item){
				if(item.id){
					// this.getsettlement(item)
					let subsidyMoney= await settlementAPI({kilometers:item.distance})
					let obj={
						  "remark": "",
						  "staffId": item.id,
						  "merchantId": item.merchantId,
						  "isAssign": 1,
						  name:item.name,
						  subsidyMoney
					}
					if(this.orderType==1){
						// this.orderStaffDO.push(obj)
					 this.$set(this.orderStaffDO, this.personIndex, obj);
					}else{
						this.orderStaffDO=[obj]
					}
					
				}else{
					 
					this.$set(this.orderStaffDO, this.personIndex, {
					  staffId: item.id, 
					  name:"随机分配"
					});
				}
				if(this.serveInfo.serviceNum==1 && this.orderStaffDO[0].staffId){
					await this.getMerAndStaCoupon(this.totalMoney,this.orderStaffDO[0].staffId)
				}else{
					this.serveInfo.discountStaffPrice=0
					this.serveInfo.staffCouponsId=0
					this.serveInfo.discountMerchantPrice=0
					this.serveInfo.merchantCouponsId=0
				}
				this.getactualMoney()
			},
			getsettlement(item){
				settlementAPI({kilometers:item.distance}).then(res=>{
					this.serveInfo.subsidyMoney=res
				})
			},
			showperson(index){
				this.personIndex=index
				if(this.adressinfo?.id){
					this.$refs.choseperson.search()
				}else{
					uni.showToast({
						title:"请先添加服务地址",
						icon:'none'
					})
				}
				
			},
			lookImg(val){
				const urls = this.images
				const current = val
				uni.previewImage({
					urls,
					current
				})
			},
			delimg(i){
				this.images.splice(i,1);			
			},
			addimg(){
				if(this.images.length<=3){
					var num=3-this.images.length
					pro.choose_and_upload(num).then(res=>{
						 
						this.images=this.images.concat(res.img_urls)
					})
				}else{
					uni.showToast({
						title: '至多上传3张图片',
						duration: 2000,
						icon:"none"
					});
				}			
			},
			showdate(type){
				this.calendarType=type
				this.$refs.calendar.visible= true
				let rangeVal=true
				if(type==2){
					rangeVal=false
				}else{
					if(this.orderType==2){
						rangeVal=false
					}else{
						rangeVal=true
					}
				}
				this.$refs.calendar.showWeek= this.orderType==1 
				this.$refs.calendar.endTimeFlag= type==2 
				this.$refs.calendar.rangeVal= rangeVal 
				
			},
			getCategoryIndex(oneCategoryName) {
			    // 定义关键字和对应的序号
			    const keywords = ['保洁', '工程', '陪护'];
			    const indices = ['1', '2', '3'];
			
			    // 遍历关键字数组，查找匹配项
			    for (let i = 0; i < keywords.length; i++) {
			        if (oneCategoryName.includes(keywords[i])) {
						// this.serveInfo.serviceNum=1
			            return indices[i];
			        }
			    }
			
			    // 如果没有找到匹配项，可以返回一个默认值，例如0
			    return 0;
			},
			getinit(){
				// 查地址
				addressdefaultlistAPI().then(res=>{
					this.SET_adressinfo(res)
					if(this.staff.staffId){
						if(!res){
							this.orderStaffDO=[{
								...this.staff,
								subsidyMoney:0 
							}]
						}else{
							settlementLongAPI({
								latitude:res.latitude,
								longitude:res.longitude,
								staffId:this.staff.staffId
							}).then(result=>{
								settlementAPI({kilometers:result}).then(rest=>{
									this.orderStaffDO=[{
										...this.staff,
										subsidyMoney:rest 
									}]
								})
								
							})
						}
						
					}
					
				})
				// 查余额
				getwalletAPI().then(res=>{
					this.wallet=res
				})
				transportationAllowanceAPI().then(res=>{
					this.transportation=res
				})
			},
			handleTime(item){
				 
				this.serveInfo.timeCycle=item.startTime+"-"+item.endTime
				// 获得一天的小时数
				 this.$set(this.serveInfo, 'serviceSection', timeToDecimal(item.workTime));
				// this.serveInfo.serviceSection=timeToDecimal(item.workTime)
				  
			} ,
			goodsDetails(){
				goodsDetailsAPI({
					categoryId: this.categoryId, 
				}).then(res => {
					this.serveInfo.categoryName = res.name
					this.money=res.money 
				})
			},
			showTimePopup() {
				this.$refs.choseTime.show = true
				 
			},
			confirm(e,chosenDays) {
				console.log(e,chosenDays) 
				this.show=false
				if(this.calendarType==2){
					this.serveInfo.deadlineTime=e[0]
					return
				}
				switch (this.orderType){
					case 1:
					case '1':
						this.serveInfo.serviceStartTime= e[0]
						this.serveInfo.serviceEndTime=e[e.length-1]	
						this.serveInfo.serviceDate=chosenDays.join(",")
						if(chosenDays.length){
							this.totalDayNum=countSpecificWeekdays(new Date(e[0]),new Date(e[e.length-1]),chosenDays)
						}else{
							this.totalDayNum=calculateDaysBetweenDates(e[0],e[e.length-1])
						}
						
						break;
					case 2:
					case '2':
						this.serveInfo.serviceStartTime= e[0]
						this.serveInfo.serviceEndTime=e[0]
						this.totalDayNum=1
						break;
					case 3:
					case '3':
						this.serveInfo.serviceStartTime= e[0]
						this.serveInfo.serviceEndTime=e[e.length-1]	
						// this.serveInfo.days=calculateDaysBetweenDates(e[0],e[e.length-1])
						this.$set(this.serveInfo,"days",calculateDaysBetweenDates(e[0],e[e.length-1]))
						this.totalDayNum=calculateDaysBetweenDates(e[0],e[e.length-1])
						break;
					default:
						break;
				}
				 
			},
			
		}
	}
</script>

<style scoped lang="scss">
.page {
	padding: 0 20rpx 150rpx;
	background-color: #F8F8F8;
	min-height: 100vh;
}

.bg {
	width: 100%;
	height: 300rpx;
	background: #277EEF;
	border-radius: 0rpx 0rpx 140rpx 140rpx;
	position: absolute;
	top: 100rpx;
	left: 0;
}
.tip{
	font-weight: 400;
	font-size: 24rpx;
	color: #999999;
	line-height: 38rpx;
	padding: 20rpx 10rpx;
}
.topprice {

	width: 100%;
	font-weight: 500;
	font-size: 28rpx;
	color: #277EEF;
	line-height: 100rpx;
	text-align: center;
}

.adress {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: space-between;
	background-color: #FFFFFF;
	border-radius: 10rpx;
	padding: 20rpx;
	margin-top: 30rpx;

	.a1 {
		font-weight: bold;
		font-size: 28rpx;
		color: #333333;
		line-height: 53rpx;
	}

	.a2 {
		font-size: 24rpx;
		color: #999999;
		line-height: 48rpx;
	}
}

.form {
	position: relative;
	margin-top: 20rpx;
	background-color: #FFFFFF;
	border-radius: 10rpx;
	padding: 20rpx;
	.list{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15rpx 0;
		.title{
			font-weight: 600;
			font-size: 28rpx;
			color: #333333;
		}
	}
	.inp {
		min-width: 200rpx; 
		display: flex;
		 
		justify-content: flex-end;
	}
	
}
 	
.upimage{
	  
	// margin-bottom: 100rpx;
	background-color: #fff;
	.title2{
		
		font-size: 30rpx;
		font-weight: 600;
		 
		line-height: 40rpx;
		text{
			font-size: 24rpx;
			font-weight: 400;
			color: #AAAAAA;
		}
		
	}
	.up3{
		margin:30rpx 0 0;
		
		display: flex;
		align-items: center;
		flex-wrap: wrap;		
		.img{
			width: 200rpx;
			height: 200rpx;
			margin-right: 20rpx;
			margin-bottom: 30rpx;
			border-radius: 8px;
			position: relative;
			.image{
				width: 200rpx;
				height: 200rpx;
			}
			.del{
				position: absolute;
				top: -12rpx;
				right: -12rpx;
				width: 30rpx;
				height: 30rpx;
				// border: 1px solid #999;
				// border-radius: 50%;
				// display: flex;
				// align-items: center;
				// justify-content: center;
			}
		}
		.add{
			width: 200rpx;
			height: 200rpx;
			background: #EEEFF2;
			border-radius: 8px;
			margin-right: 30rpx;
			margin-bottom: 30rpx;
			border: 1px dashed #c2c6d1;
			display: flex;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;
			view{
				color: #7C8191;
				 
				text-align: center;
				// font-weight: bold;
				font-size: 68rpx;
			}
		}
	}
}

.orderfooter {
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 20rpx 30rpx;
	// height: 120rpx;
	z-index: 9;
    background-color: #fff;
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);
    text {
      font-size: 24rpx;
      color: #666;
    }
    
    .fn-col-left {
      flex-grow: 1;
	  view{
		  font-weight: 400;
		  font-size: 24rpx;
		  color: #666666;
	  }
      .col-left {
        display: inline-block;
        width: 45%;
        height: 80rpx;
        color: red;
      }
      .col-right {
        display: inline-block;
        width: 55%;
        height: 80rpx;
      }
      .price {
        font-size: 40rpx;
        font-weight: bold;
        line-height: 47rpx;
        color: $uni-color-primary;
      }
      .price-number {
        padding-left: 10rpx;
      }
      .price-detaile {
        width: 180rpx;
        height: 40rpx;
        padding: 0px 10rpx;
        font-size: 24rpx;
        color:$uni-color-primary;
        text-align: center;
        background: #F2F7FE;
        border-radius: 60rpx 60rpx 60rpx 60rpx;
      }
    }
}	
.popup {
    padding:10rpx 30rpx 120rpx; 
	.tip{
		display: flex;
		align-items: center;
		justify-content: center;
		image{
			width: 30rpx;
			margin-right: 10rpx;
		}
		text{
			font-weight: bold;
			font-size: 38rpx;
			color: #333333;
		}
	}
	.box{
		 
		border-radius: 15rpx;
		background-color: #fff;
		margin: 30rpx 0;
		 
		.a{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin:30rpx 0;
			
			view{
				
				font-weight: bold; 
				font-size: 28rpx;
				
			}
			text{
				color: #333333;
				 
				font-size: 28rpx;
			}
			.rmb{
				color: #277EEF;
				margin-right: 1rpx;
			}
		}
	}	 
	
}
 
</style>
<style>
	.u-radio-group--row{
		justify-content: flex-end !important;
	}
</style>